#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>

#include "./../../inc/homepage.h"
#include "./../../inc/msg_popup.h"
#include "../inc/user_event.h"
#include "../inc/cJSON.h"
#include "../inc/tcp.h"

// static int chat_room_flag = 0; // 0：不是聊天室页面 1: 已经是聊天室页面

#define ULGRAY 0xF5F5F5
#define LDWHITE 0xF6F6F6

static void chat_room_box_click_cb(lv_event_t * e);

void chat_room_init(lv_obj_t * parent)
{
    lv_obj_t * chat_room_box = lv_obj_create(parent);
    lv_obj_set_size(chat_room_box, LV_PCT(100), LV_PCT(10));
    lv_obj_set_pos(chat_room_box, 0, LV_PCT(0));
    lv_obj_set_style_radius(chat_room_box, 0, 0);
    lv_obj_set_style_border_width(chat_room_box, 0, 0);
    // lv_obj_set_style_border_width(chat_room_box, 1, 0);
    // lv_obj_set_style_border_color(chat_room_box, lv_color_hex(WHITE), 0);
    lv_obj_set_style_pad_all(chat_room_box, 0, 0);

    lv_obj_add_event_cb(chat_room_box, chat_room_box_click_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t * chat_room_box_title = lv_label_create(chat_room_box);
    lv_label_set_text(chat_room_box_title, "聊天室频道");
    lv_obj_center(chat_room_box_title);
    lv_obj_set_style_text_color(chat_room_box_title, lv_color_hex(BLACK), 0);
    lv_obj_set_style_text_font(chat_room_box_title, &SourceHanSansCN_Bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);

    lv_obj_t * chat_room_box_title_underline = lv_obj_create(chat_room_box);
    lv_obj_set_pos(chat_room_box_title_underline, 0, LV_PCT(99));
    lv_obj_set_size(chat_room_box_title_underline, LV_PCT(100), 1);
    lv_obj_set_style_bg_color(chat_room_box_title_underline, lv_color_hex(ULGRAY), 0);
    lv_obj_set_style_radius(chat_room_box_title_underline, 0, 0);
    lv_obj_set_style_border_width(chat_room_box_title_underline, 0, 0);
}

// 实现聊天室容器的点击事件回调函数
static void chat_room_box_click_cb(lv_event_t * e)
{
    if(user_event.type != NULL) printf("chat_room_box_click_cb   %s\n", user_event.type);
    if(strcmp(user_event.type, "chat_room") == 0) {
        return;
    }
    printf("chat_room_box_click_cb    over\n");
    strcpy(user_event.type, "chat_room");
    lv_obj_t * clicked_box = lv_event_get_target(e);
    if(user_event.type_friend_item != NULL) {
        lv_obj_set_style_bg_color(user_event.type_friend_item, lv_color_hex(WHITE), 0);
    }
    user_event.type_friend_item = clicked_box;
    lv_obj_set_style_bg_color(clicked_box, lv_color_hex(ULGRAY), 0);

    create_chat_box(home_src);
    lv_label_set_text(char_box_title, "聊天室频道");

    printf("chat_room_box_click_cb  create_chat_box   over\n");
    cJSON * root = cJSON_CreateObject();
    if(root == NULL) {
        printf("创建JSON根对象失败\n");
        return;
    }

    // 添加type字段
    cJSON_AddStringToObject(root, "type", "join_chat_room");

    cJSON * payload = cJSON_CreateObject();
    if(payload == NULL) {
        printf("创建payload对象失败\n");
        cJSON_Delete(root);
        return;
    }

    cJSON_AddStringToObject(payload, "userid", user_event.userid); // current_user_id为当前登录用户ID

    cJSON_AddStringToObject(payload, "action", "join");
    cJSON_AddItemToObject(root, "payload", payload);

    // 转换JSON为字符串，用于发送
    char * json_str = cJSON_PrintUnformatted(root);
    if(json_str == NULL) {
        printf("JSON转换为字符串失败\n");
        cJSON_Delete(root);
        return;
    }

    // 打印调试信息
    printf("打包的聊天室JSON: %s\n", json_str);

    send_message_to_server(&server_info, json_str);

    // 释放资源
    cJSON_Delete(root);
    free(json_str);
}
